<?php

/**
 * This is the model class for table "user_role".
 *
 * The followings are the available columns in table 'user_role':
 * @property integer $id_user_role
 * @property integer $id_user_role_perm
 * @property string $name
 * @property string $description
 * @property integer $supreme
 */
class UserRole extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return UserRole the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user_role';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name', 'required'),
			array('name', 'unique', 'className' => __CLASS__, 'attributeName' => 'name', 'message' => 'name already exists'),
			array('id_user_role_perm', 'numerical', 'integerOnly'=>true),
			array('name, description', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_user_role, id_user_role_perm, name, description, supreme', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			// 1. has many users
			
			'UserRolePerm' => array(self::HAS_ONE, 'UserRolePerm', array('id_user_role_perm' => 'id_user_role_perm')),
		
			'UserRolePermAccount' => array(self::HAS_MANY, 'UserRolePermAccount', array('id_user_role' => 'id_user_role')),
			'UserRolePermAccountAllow' => array(self::HAS_MANY, 'UserRolePermAccount', array('id_user_role' => 'id_user_role'), 'on' => 'allow_or_deny=1'),
			'UserRolePermAccountDeny' => array(self::HAS_MANY, 'UserRolePermAccount', array('id_user_role' => 'id_user_role'), 'on' => 'allow_or_deny=0'),
		
			'UserRolePermGroup' => array(self::HAS_MANY, 'UserRolePermGroup', array('id_user_role' => 'id_user_role')),
			'UserRolePermGroupAllow' => array(self::HAS_MANY, 'UserRolePermGroup', array('id_user_role' => 'id_user_role'), 'on' => 'allow_or_deny=1'),
			'UserRolePermGroupDeny' => array(self::HAS_MANY, 'UserRolePermGroup', array('id_user_role' => 'id_user_role'), 'on' => 'allow_or_deny=0')
		);
	}
	
	public function delete()
	{
		if($this->supreme == 1){
			$this->addError('Supreme', 'Supreme Admin Role cannot be deleted');
			return false;
		}
		
		if(parent::delete()){
			// delete permission and access of this role
			if(!empty($this->UserRolePerm)){
				$this->UserRolePerm->delete();
			}
			UserRolePermAccount::model()->deleteAllByAttributes(array('id_user_role' => $this->id_user_role));
			UserRolePermGroup::model()->deleteAllByAttributes(array('id_user_role' => $this->id_user_role));
			
			// make users under this role have an empty user_role 
			app()->db->createCommand()->update(
				User::model()->tableName(),
				array('id_user_role' => 0),
				'id_user_role = :pk',
				array(':pk' => $this->id_user_role)
			);
			
			return true;
		}
		
		return false;
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
        return array(
            'id_user_role' => 'Id User Role',
            'id_user_role_perm' => 'Id User Role Perm',
            'name' => 'Name',
            'description' => 'Description',
            'supreme' => 'Supreme',
        );
	}

	public function __get($key)
	{
		$action = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
		if(method_exists($this, $action)){
			return $this->$action();
		}
		
		return parent::__get($key);
	}
	public function getNameDropdownLabel()
	{
		return 'Role: ' . $this->name;
	}
	
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

        $criteria->compare('id_user_role',$this->id_user_role);
        $criteria->compare('id_user_role_perm',$this->id_user_role_perm);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('description',$this->description,true);
//        $criteria->compare('supreme',$this->supreme);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination' => array(
				'pageSize'=> app()->user->getData()->getIndividualConfig()->getPagination()
			)
		));
	}
	
	public static function renderListActionButtons()
	{
		return '
			<div class="btn-group">
				<button class="btn btn-info dropdown-toggle actions" data-toggle="dropdown">
					'. t('buttons', 'Actions') .'
					<span class="caret"></span>
				</button>
				<ul class="dropdown-menu">
					<li class="user_role_edit"><a href="#">'. t('buttons', 'Edit User Role') .'</a></li>
					<li class="user_role_delete"><a href="#">'. t('buttons', 'Delete User Role') .'</a></li>
				</ul>
				
				<div class="delete_confirm_btn hide">
					<a class="btn btn-small delete_confirm"><i class="icon-ok"></i>'. t('buttons', 'Confirm Delete') .'</a>
					<a class="btn btn-small delete_cancel"><i class="icon-remove"></i>'. t('buttons', 'Cancel') .'</a>
				</div>
			</div>
		';
	}
}